<?php
namespace home\controllers;

use home\models\Products;
use home\models\search\Product as ProductSearch;
use Yii;
use yii\web\NotFoundHttpException;

/**
 * Class ProductController
 *
 * @package home\controllers
 */
class ProductController extends BaseController
{

    /**
     * @return string
     */
    public function actionIndex()
    {
        $searchModel = new ProductSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

        $data = $dataProvider->getModels();
        $pager = $dataProvider->getPagination();

        return $this->render('index', [
            'searchModel' => $searchModel,
            'data' => $data,
            'pager' => $pager,
            'sample' => Yii::$app->request->get('category_id', '') === '',
        ]);
    }

    /**
     * @param $id
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionView($id)
    {
        $model = Products::findOne($id);

        if(!$model){
            throw new NotFoundHttpException('产品不存在！');
        }

        return $this->render('view', ['model' => $model]);
    }
}
